

package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class RatingCommentManagement {
	
    def listRatingComments() {
        def ratingComments = RatingComment.list()
        JSONArray arr = new JSONArray()
        ratingComments.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("creationDate",it.creationDate)
            obj.put("rating",it.rating)
            obj.put("comment",it.comment)
            obj.put("customerId",it.customerId)
            obj.put("productId",it.productId)
            arr.add(obj)
        }
        
        return arr
    }
    
    def listRatingCommentsByCustomerID(custId) {
        String selectedCustId = custId.toString()
        def ratingComments = RatingComment.list()
        JSONArray arr = new JSONArray()
        ratingComments.each() {
            String customerId = it.customer.id.toString()
            if (customerId.equals("selectedCustId")) {
                JSONObject obj = new JSONObject()
                obj.put("id",it.id)
                obj.put("creationDate",it.creationDate)
                obj.put("rating",it.rating)
                obj.put("comment",it.comment)
                obj.put("customerId",it.customerId)
                obj.put("productId",it.productId)
                arr.add(obj)
            }
        }
        
        return arr
    }
    
    def listRatingCommentsByProductID(prodId) {
        String selectedProdId = prodId.toString()
        def ratingComments = RatingComment.list()
        JSONArray arr = new JSONArray()
        ratingComments.each() {
            String productId = it.product.id.toString()
            if (productId.equals("selectedProdId")) {
                JSONObject obj = new JSONObject()
                obj.put("id",it.id)
                obj.put("creationDate",it.creationDate)
                obj.put("rating",it.rating)
                obj.put("comment",it.comment)
                obj.put("customerId",it.customerId)
                obj.put("productId",it.productId)
                arr.add(obj)
            }
        }
        
        return arr
    }
    
    def getRatingCommentByID(id) {
        def rc = RatingComment.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",rc.id)
        obj.put("creationDate",rc.creationDate)
        obj.put("rating",rc.rating)
        obj.put("comment",rc.comment)
        obj.put("customerId",rc.customerId)
        obj.put("productId",rc.productId)
        
        return obj
    }
    
    def getRatingCommentByCustomerIDAndProductID(custId, prodId) {
        String selectedCustId = custId.toString()
        String selectedProdId = prodId.toString()
        
        def ratingComments = RatingComment.list()
        ratingComments.each() {
            String customerId = it.customer.id.toString()
            String productId = it.product.id.toString()
            if (customerId.equals("selectedCustId")) {
                if (productId.equals("selectedProdId")) {
                    JSONObject obj = new JSONObject()
                    obj.put("id",it.id)
                    obj.put("creationDate",it.creationDate)
                    obj.put("rating",it.rating)
                    obj.put("comment",it.comment)
                    obj.put("customerId",it.customerId)
                    obj.put("productId",it.productId)

                    return obj
                }
            }
        }  
        
        return null
    }

//    def getRatingCommentForDbByTypeName(ratingCommentName) {
//        def rc = RatingComment.findByTypeName(ratingCommentName)
//        
//        return rc
//    }
    
    def addRatingComment(o) {
        def status
        
        println "rating:: " + o.get("rating")
        println "comment:: " + o.get("comment")
        
        CustomerManagement cman = new CustomerManagement()
        ProductManagement pman = new ProductManagement()
        
        RatingComment rc = new RatingComment()
        Date d = new Date();
        rc.creationDate = d
        if (o.get("rating") == "") {
            rc.rating = "-"
        }
        else {
            rc.rating = o.get("rating").toInteger()
        }
        if (o.get("comment") == "") {
            rc.comment = "-"
        }
        else {
            rc.comment = o.get("comment")
        }
        def c = cman.getCustomerForDbByCustomerID(o.get("customer"))
        if (c != null) {
            rc.customer = c
        }
        def p = pman.getProductForDbByProductID(o.get("product"))
        if (p != null) {
            rc.product = p
        }

        if (rc.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add Rating and Comments >> ' + status
        
        return status
    }
    
    def updateRatingComment(ratingComment) {
        def status
        
        RatingComment rc = RatingComment.findById(ratingComment.id)
        if (ratingComment.has("rating")) { rc.rating = ratingComment.rating }
        if (ratingComment.has("comment")) { rc.comment = ratingComment.comment }
        
        if (rc.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update Rating and Comments >> ' + status
        
        return status
    }
    
    def deleteRatingComment(id) {
        def status
        
        RatingComment rc = RatingComment.findById(id)
        if (rc.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete Rating and Comments >> ' + status
        
        return status
    }
}

